package com.bjb.vr.manager.controller;

import com.bjb.vr.common.ao.ResPlaylistAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.ResPlaycontentService;
import com.bjb.vr.common.service.ResPlaylistService;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.UmsAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 播放列表
 *
 * @author maxingchen
 * @version 1.0
 * @date 2022/3/24 4:40 下午
 */
@RestController
@RequestMapping("/api/manager/v1")
public class PlaylistController {

    @Autowired
    ResPlaylistService playlistService;
    @Autowired
    ResPlaycontentService playcontentService;

    /**
     * 根据条件查询课集列表
     *
     * @param resPlaylistAo
     * @param authorization
     * @return
     */
    @PostMapping("/queryPlaylist")
    public BaseResult<ResPlaylistAo> queryPlaylist(@RequestBody ResPlaylistAo resPlaylistAo, @RequestHeader String authorization ){
        return playlistService.queryPlaylist(resPlaylistAo,authorization);
    }

    /**
     * 新增课集列表
     *
     * @param resPlaylistAo
     * @return
     */
    @PostMapping("/addPlaylist")
    public BaseResult addPlaylist(@RequestBody ResPlaylistAo resPlaylistAo){
        //列表名称不能重复处理
        UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
        boolean exists = playlistService.queryPlaylistByIdAndName(umsAccountVo.getUserId(),resPlaylistAo.getPlayName());
        if (exists) {
            return CommonResult.Fail("课程清单名称已存在");
        }
        boolean success = playlistService.addPlaylist(resPlaylistAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }

    /**
     * 修改课集列表名字
     *
     * @param resPlaylistAo
     * @param authorization
     * @return
     */
    @PostMapping("/editPlaylist")
    public BaseResult editPlaylist(@RequestBody ResPlaylistAo resPlaylistAo,@RequestHeader String authorization ){
        //列表名称不能重复处理
        UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
        boolean exists = playlistService.queryPlaylistByIdAndName(umsAccountVo.getUserId(),resPlaylistAo.getPlayName());
        if (exists) {
            return CommonResult.Fail("课程清单名称已存在");
        }
        boolean success = playlistService.editPlaylist(resPlaylistAo,authorization);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 修改课集里面的课程
     *
     * @param resPlaylistAo
     * @param authorization
     * @return
     */
    @PostMapping("/editlistPlayIds")
    public BaseResult editlistPlayIds(@RequestBody ResPlaylistAo resPlaylistAo,@RequestHeader String authorization ){
        boolean success = playlistService.editlistPlayIds(resPlaylistAo,authorization);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 删除课集列表
     *
     * @param resPlaylistAo
     * @param authorization
     * @return
     */
    @PostMapping("/removePlaylist")
    public BaseResult removePlaylist(@RequestBody ResPlaylistAo resPlaylistAo,@RequestHeader String authorization ){
        boolean success = playlistService.removePlaylist(resPlaylistAo.getIds(),authorization);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }


}
